package com.ailink.concurrent.queue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2018/10/20
 */
public class ArrayBlockingQueueTest {
    public static void main(String[] args) {
        long beginTime = System.currentTimeMillis();
        int count = 10000000;
        ArrayBlockingQueue<Integer> data = new ArrayBlockingQueue<>(count);
        for (int i = 0; i < count; i++) {
            data.add(i);
        }
        long endTime = System.currentTimeMillis();
        System.out.println(endTime - beginTime);

        long beginTime1 = System.currentTimeMillis();
        ExecutorService executorService = Executors.newFixedThreadPool(1000);
        for (int i = 0; i < 1000; i++) {
            executorService.execute(() -> {
                while (true) {
                    try {
                        Integer result = data.poll(5, TimeUnit.SECONDS);
                        if (result == null) break;
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        while (true) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            int size = data.size();
            if (size == 0) break;
        }
        long endTime1 = System.currentTimeMillis();
        System.out.println(endTime1 - beginTime1);
        executorService.shutdown();
    }
}
